//
//  SINATecApprovalManagerDetailViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2018/1/9.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINATecApprovalManagerDetailViewController.h"
#import "SINACircularBeadUtils.h"
#import "SINACalendarUtils.h"
#import "SINALabelUtils.h"

@interface SINATecApprovalManagerDetailViewController ()<UIScrollViewDelegate>



//状态
@property (weak, nonatomic) IBOutlet UILabel *statueLabel;
//状态指示
@property (weak, nonatomic) IBOutlet UIImageView *flagImageView;
//驳回
@property (weak, nonatomic) IBOutlet UILabel *noPassLabel;
@property (weak, nonatomic) IBOutlet UILabel *passLabel;
//操作按钮区间
@property (weak, nonatomic) IBOutlet UIView *commitView;
//请假人
@property (weak, nonatomic) IBOutlet UILabel *stuNameLabel;

@property(strong,nonatomic) SINATecManagerLeaveModel  *currentLeaveModel;

//请假次数
@property (weak, nonatomic) IBOutlet UILabel *leaveCountLabel;
//学院
@property (weak, nonatomic) IBOutlet UILabel *collegeNameLabel;
//班级
@property (weak, nonatomic) IBOutlet UILabel *classNameLabel;

//请假类型
@property (weak, nonatomic) IBOutlet UILabel *leaveTypeLabel;

@property (weak, nonatomic) IBOutlet UILabel *startTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *endTimeLabel;

//请假事由
@property (weak, nonatomic) IBOutlet UILabel *leaveResonLabel;

@property (weak, nonatomic) IBOutlet UILabel *approveTecLabel;

@property (weak, nonatomic) IBOutlet UILabel *approveTimeLabel;

//附件
@property (weak, nonatomic) IBOutlet UIView *otherFileMainView;
@property (weak, nonatomic) IBOutlet UIView *otherFileContentView;
//审批人
@property (weak, nonatomic) IBOutlet UIView *checkPeopleView;
//审批人详情
@property (weak, nonatomic) IBOutlet UIView *checkPeopleDesView;
@property (weak, nonatomic) IBOutlet UIView *resonMainView;

@property(nonatomic,assign) NSInteger imageWidth;
@property(nonatomic,assign) NSInteger imageRowWidth;

@property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView;


@property (weak, nonatomic) IBOutlet UILabel *noFileLabel;
@property (weak, nonatomic) IBOutlet UILabel *resonDesLabel;



@property (nonatomic, assign) UIView *backgroundView;//图片放大
@property (nonatomic, assign) UIImageView *browseImgView;
@end

@implementation SINATecApprovalManagerDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title=@"审批详情";
    
    self.statueLabel.hidden=YES;
    self.flagImageView.hidden=YES;
    self.commitView.hidden=YES;
    
    self.imageWidth=76;
    self.imageRowWidth=(mainW-30-4*self.imageWidth)/3;
    
    self.contentScrollView.delegate=self;
    
    
    [self loadingSubClassLeaveAskListNetData];
    
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.passLabel andWithBlick:^(UIView *view) {
        [self stuLeavelAskApprove:1004];
    }];
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.noPassLabel andWithBlick:^(UIView *view) {
        [self stuLeavelAskApprove:1005];
    }];
    
    
    self.resonDesLabel.frame = CGRectMake(CGRectGetMaxX(self.leaveResonLabel.frame), 14, CGRectGetWidth(self.resonMainView.frame)-CGRectGetMaxX(self.leaveResonLabel.frame)-12, 20);
    
}

-(void)viewWillLayoutSubviews{
    [self initViewFrameFunction];
}

-(void) initViewFrameFunction{
    
    DLog(@"重新排列");
    if (self.currentLeaveModel!=nil) {
        [self updateDataShowFunction];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


//加载请假详情信息
-(void)loadingSubClassLeaveAskListNetData{

    [self startLoading];
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    
    ////审批记录的uuid String approvaluuid
    
    [dict setObject:self.model.uuid forKey:@"approvaluuid"];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@",baseUrl,getApprovalInfoByTypeUrl] withParamters:dict success:^(id responseData) {
        
        NSString *code = responseData[@"code"];
        [weakSelf stopLoading];
        if ([code isEqualToString:@"1000"]) {
            
            NSDictionary *result = responseData[@"content"];
            
            if (result!=nil) {
                
                SINATecManagerLeaveModel  *model = [[SINATecManagerLeaveModel alloc]initWithDict:result];
                
                self.currentLeaveModel = model;
                
                [self updateDataShowFunction];
                
            }else{
                [YPXToastView showToastViewWithText:@"暂无班级数据 " andDuration:3 andParentView:self.statueLabel];
            }
            
        }else{
//            NSString *message =responseData[@"message"];
//            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
//                                                                 cancleBtn:nil];
            //[alertView showXLAlertView];
            //self.noDataLabel.hidden=NO;
            //self.mainContentTableView.hidden=YES;
            [YPXToastView showToastViewWithText:@"加载班级数据异常 " andDuration:3 andParentView:self.statueLabel];
        }
   
    } failure:^(NSError *error, NSInteger errCode) {
        DLog(@"加载 失败  %@ ",error.debugDescription);
        [weakSelf stopLoading];
//        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"加载数据失败" sureBtn:@"确定"
//                                                             cancleBtn:nil];
        //[alertView showXLAlertView];
        //        self.noDataLabel.hidden=NO;
        //        self.mainContentTableView.hidden=YES;
        [YPXToastView showToastViewWithText:@"加载班级数据异常 " andDuration:3 andParentView:self.statueLabel];
    }];
}


//审批详情
-(void)stuLeavelAskApprove:(NSInteger) index{
   
    [self startLoading];
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    SINAUserModel *userModel=[[SINACommonUserHelper share]getUserModel];
    //审批记录的uuid String approvaluuid
    //审批状态 String status:同意 1004，拒绝 1005
    [dict setObject:self.model.uuid forKey:@"approvaluuid"];
    [dict setObject:[NSString stringWithFormat:@"%@",userModel.userRealName] forKey:@"tecname"];
    
    [dict setObject:[NSString stringWithFormat:@"%ld",(long)index] forKey:@"status"];
    
    NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]init];
    
    [dict2 setObject:[SINAHttpRequestUtils encodeDicParams:dict] forKey:@"sj"];
    
    WeakSelf;
    
    [CXAFNHttpRequestUtils httpPostRequest:[NSString stringWithFormat:@"%@%@",baseUrl,techApprovalUrl] withParamters:dict2 success:^(id responseData) {
        
        NSString *code = responseData[@"code"];
        [weakSelf stopLoading];
        if ([code isEqualToString:@"1000"]) {
           
            // 2.创建通知
            
            NSNotification *notification =[NSNotification notificationWithName:@"updateStuLeaveListNotification" object:nil userInfo:nil];
            // 3.通过 通知中心 发送 通知
            [[NSNotificationCenter defaultCenter] postNotification:notification];
            
            [self loadingSubClassLeaveAskListNetData];
          
        }else{
//            NSString *message =responseData[@"message"];
//            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
//                                                                 cancleBtn:nil];
            //[alertView showXLAlertView];
            //self.noDataLabel.hidden=NO;
            //self.mainContentTableView.hidden=YES;
            [YPXToastView showToastViewWithText:@"加载班级数据异常 " andDuration:3 andParentView:self.statueLabel];
        }

    } failure:^(NSError *error, NSInteger errCode) {
        DLog(@"加载 失败  %@ ",error.debugDescription);
        [weakSelf stopLoading];
//        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"加载数据失败" sureBtn:@"确定"
//                                                             cancleBtn:nil];
        //[alertView showXLAlertView];
        //        self.noDataLabel.hidden=NO;
        //        self.mainContentTableView.hidden=YES;
        [YPXToastView showToastViewWithText:@"加载班级数据异常 " andDuration:3 andParentView:self.statueLabel];
    }];
}


-(void) updateDataShowFunction{
    self.stuNameLabel.text=[NSString stringWithFormat:@"请假人：%@",self.currentLeaveModel.leavename];
    
    self.leaveCountLabel.text=[NSString stringWithFormat:@"请假次数：%@",self.currentLeaveModel.cnt];
    
    self.collegeNameLabel.text=[NSString stringWithFormat:@"学院：%@",self.currentLeaveModel.collegename];
    
    self.classNameLabel.text=[NSString stringWithFormat:@"班级：%@",self.currentLeaveModel.classifyname];
    
    self.leaveTypeLabel.text=[NSString stringWithFormat:@"请假类型：%@",self.currentLeaveModel.leaveTypeName];
    
    self.startTimeLabel.text = [NSString stringWithFormat:@"开始时间：%@",[SINACalendarUtils getDateStringWithTimeStr:self.currentLeaveModel.starttime andFormat:@"yyyy-MM-dd HH:mm"]];
    self.endTimeLabel.text = [NSString stringWithFormat:@"结束时间：%@",[SINACalendarUtils getDateStringWithTimeStr:self.currentLeaveModel.endtime andFormat:@"yyyy-MM-dd HH:mm"]];
    
    
    self.resonDesLabel.text=[NSString stringWithFormat:@"%@",self.currentLeaveModel.reason];
    
    [SINALabelUtils updateLabel: self.resonDesLabel andWithString: self.resonDesLabel.text andWithTagWidth:mainW-110-30 andWithTagHeight:44 andWithTagFontSize:15];
    if (CGRectGetHeight(self.resonDesLabel.frame)>30) {
        //两行以上
        self.resonMainView.frame=CGRectMake(0, self.resonMainView.frame.origin.y, mainW, 30+CGRectGetHeight(self.resonDesLabel.frame));
    } else {
        //一行
        self.resonMainView.frame=CGRectMake(0, self.resonMainView.frame.origin.y, mainW, 66);
    }
    
    self.approveTecLabel.text=[NSString stringWithFormat:@"%@",self.currentLeaveModel.approvalname];
    
    NSInteger index = self.currentLeaveModel.status;
    if (index==1005||index==1004) {
        self.approveTimeLabel.text=[NSString stringWithFormat:@"%@",[[SINACalendarUtils alloc] getStandardTimeInterval:[self.currentLeaveModel.approvaltime integerValue]/1000]];
    }else{
        self.approveTimeLabel.text=@"";
    }
    
    
    [self setStatueFunction];
    
    NSMutableArray *array = self.currentLeaveModel.imageUrlArray;
    if (array==nil) {
        DLog(@"暂无附件");
        self.noFileLabel.hidden = NO;
    } else {
        self.noFileLabel.hidden = YES;
        DLog(@"有附件 %ld" ,(long)array.count);
        self.otherFileContentView.hidden=NO;
        for (NSInteger i=0; i<array.count; i++) {
            NSString *imageUrl = array[i];
            
            UIView *itemView = [[UIView alloc]init];
            
            itemView.frame=CGRectMake(i%4*(self.imageWidth+self.imageRowWidth), i/4*(self.imageWidth+15*i/4), self.imageWidth, self.imageWidth);
            
            [self.otherFileContentView addSubview:itemView];
            
            
            UIImageView *itemImageView  = [[UIImageView alloc]init];
            
            itemImageView.frame=CGRectMake(0,0, self.imageWidth, self.imageWidth);
            
            [itemView addSubview:itemImageView];
            
            [itemImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
            
            
            [[SINAUiviewClickLiserner alloc]viewClickFunction:itemView andWithBlick:^(UIView *view) {
                DLog(@"查看大图");
                
                //创建一个黑色背景
                //初始化一个用来当做背景的View。
                UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, mainW, mainH)];
                self.backgroundView = bgView;
                [bgView setBackgroundColor:[UIColor colorWithRed:0/250.0 green:0/250.0 blue:0/250.0 alpha:1.0]];
                //[self.view addSubview:bgView];
                UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
                [rootWindow addSubview:bgView];
                //创建显示图像的视图
                //初始化要显示的图片内容的imageView
                UIImageView *browseImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, mainW, mainH - 64)];
                browseImgView.contentMode = UIViewContentModeScaleAspectFit;
                self.browseImgView = browseImgView;
                //要显示的图片，即要放大的图片
                [self.browseImgView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
                [bgView addSubview:browseImgView];
                
                
                
                
                [self shakeToShow:bgView];
                
                 [[SINAUiviewClickLiserner alloc]viewClickFunction:bgView andWithBlick:^(UIView *view) {
                     [self closeView];
                     }];
            }];
            
        }
        
        
        
    }
    
    self.contentScrollView.contentSize=CGSizeMake(mainW,CGRectGetMaxY(self.commitView.frame));
    
    
}
-(void)closeView{
   
     [self.backgroundView removeFromSuperview];
}
//放大过程中出现的缓慢动画
- (void) shakeToShow:(UIView*)aView{
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.2;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    [aView.layer addAnimation:animation forKey:nil];
}


-(void)setStatueFunction{
    NSInteger index = self.currentLeaveModel.status;
    if (index==1005) {
        //未通过
        [SINACircularBeadUtils makeBorder:self.statueLabel andWithBorderWidth:1 andBorderRadius:1 andBorderColor:[UIColor redColor]];
        self.statueLabel.text=@"已驳回";
        self.statueLabel.hidden=NO;
        self.flagImageView.hidden=NO;
        self.commitView.hidden=YES;
        self.flagImageView.image=[UIImage imageNamed:@"ask_leave_no"];
        [self.statueLabel setTextColor:[UIColor redColor]];
        
    }else  if (index==1004) {
        //已通过
        [SINACircularBeadUtils makeBorder:self.statueLabel andWithBorderWidth:1 andBorderRadius:1 andBorderColor:baseSelectColor];
        self.statueLabel.text=@"已通过";
        self.statueLabel.hidden=NO;
        self.flagImageView.hidden=NO;
        self.commitView.hidden=YES;
        [self.statueLabel setTextColor:baseSelectColor];
    }else  {
        
        //未审批
        [SINACircularBeadUtils makeBorder:self.statueLabel andWithBorderWidth:1 andBorderRadius:1 andBorderColor:baseSelectColor];
        self.statueLabel.text=@"未审批";
        self.statueLabel.hidden=YES;
        self.flagImageView.hidden=YES;
        self.commitView.hidden=NO;
        [SINACircularBeadUtils makeCircularBead:1 andView:self.passLabel andIsSetBackground:NO];
        [SINACircularBeadUtils makeCircularBead:1 andView:self.noPassLabel andIsSetBackground:NO];
        
        if (self.flag==1) {
            self.commitView.hidden=YES;
        } else {
            self.commitView.hidden=NO;
        }
        
        
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self updateDataShowFunction];
}
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self updateDataShowFunction];
}

@end
